package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ChapterAssetsMapper;
import com.think.teaching.model.ChapterAssets;
import com.think.teaching.service.ChapterAssetsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课件 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ChapterAssetsServiceImpl extends BaseServiceImpl<ChapterAssetsMapper, ChapterAssets>
		implements ChapterAssetsService {

	@Override
	public ChapterAssets AddChapterAssets(ChapterAssets chapterAssets) {
		if (this.save(chapterAssets)) {
			return chapterAssets;
		}
		return null;
	}

	@Override
	public ChapterAssets UpdateChapterAssets(ChapterAssets chapterAssets) {
		if (this.updateById(chapterAssets)) {
			return chapterAssets;
		}
		return null;
	}

	@Override
	public ChapterAssets GetChapterAssets(Integer id) {
		QueryWrapper<ChapterAssets> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public List<ChapterAssets> SearchChapterAssetsWithKey(String key, Boolean withoutBan) {
		QueryWrapper<ChapterAssets> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("content", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public IPage<ChapterAssets> ListChapterAssetsWithTagAndType(Integer count
			, Integer page,Integer courseType,Integer type,
			Boolean withoutBan) {
		QueryWrapper<ChapterAssets> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if(courseType!=null)
			queryWrapper.eq("course_type",courseType);
		if(type!=null)
			queryWrapper.eq("type",type);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<ChapterAssets> ipage = new Page<>(page, count);
		return this.page(ipage,queryWrapper);
	}

	@Override
	public ChapterAssets SetChapterAssetsState(Integer id, Integer state) {
		UpdateWrapper<ChapterAssets> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
}
